package com.xd.server.sync.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xd.common.response.ResponseObject;
import com.xd.common.response.ResponseResult;
import com.xd.server.sync.dto.DataSyncLogDTO;
import com.xd.server.sync.service.IDataSyncLogService;
import com.xd.server.sync.vo.DataSyncLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据同步日志(DataSyncLog)表控制层
 *
 * @author xiaodi
 * @since 2023-03-28 16:51:03
 */
@Api(value = "com.xd.server.sync.controller.DataSyncLogController", tags = {"数据同步日志管理"})
@RestController
@RequestMapping("/dataSyncLog")
public class DataSyncLogController {
    /**
     * 服务对象
     */
    @Resource
    private IDataSyncLogService dataSyncLogService;

    /**
     * 保存一个
     *
     * @param dataSyncLogDTO 传输类DTO
     * @returns com.xd.common.response.ResponseObject
     */
    @PostMapping("/save-or-update")
    @ApiOperation(value = "保存或更新", notes = "保存或更新")
    public ResponseObject<Object> saveOrUpdate(@RequestBody @Validated DataSyncLogDTO dataSyncLogDTO) {
        int i = dataSyncLogService.saveOrUpdateDataSyncLog(dataSyncLogDTO);
        if (i < 0) {
            return ResponseResult.error("操作失败");
        }
        return ResponseResult.success();
    }

    /**
     * 删除一个
     *
     * @param id id主键
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("/delete")
    @ApiOperation(value = "删除", notes = "根据主键删除")
    public ResponseObject<Object> delete(@ApiParam(value = "主键", name = "id", required = true, example = "1")
                                         @Length(message = "id不得超过64位", min = 1, max = 64)
                                         @RequestParam("id") String id) {
        int i = dataSyncLogService.deleteDataSyncLog(id);
        if (i < 0) {
            return ResponseResult.error("操作失败");
        }
        return ResponseResult.success();
    }


    /**
     * 查询一个
     *
     * @param id 主键
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("/findOne")
    @ApiOperation(value = "查询一个", notes = "根据主键查询")
    public ResponseObject<DataSyncLogVO> selectOne(@ApiParam(name = "id", value = "主键", required = true, example = "1")
                                                   @Length(message = "id长度不得超过64位", min = 1, max = 64)
                                                   @RequestParam(value = "id") String id) {

        return ResponseResult.success(dataSyncLogService.findDataSyncLog(id));
    }

    /**
     * 查询所有
     *
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有", notes = "查询所有")
    public ResponseObject<List<DataSyncLogVO>> selectAll() {
        List<DataSyncLogVO> allDataSyncLog = dataSyncLogService.findAllDataSyncLog();
        return ResponseResult.success(allDataSyncLog.size(), allDataSyncLog);
    }

    /**
     * 查询所有-分页
     *
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("/findPage")
    @ApiOperation(value = "查询所有-分页", notes = "查询所有-分页")
    public ResponseObject<List<DataSyncLogVO>> findPage(@ApiParam DataSyncLogDTO dataSyncLogDTO) {
        Page<DataSyncLogVO> page = dataSyncLogService.findPageDataSyncLog(dataSyncLogDTO);
        return ResponseResult.success(page.getTotal(), page.getRecords());
    }

}
